/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pas.business.model;

import za.co.pas.business.abstracts.AbstractDataModel;
import java.util.Calendar;
import java.util.List;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
import javax.persistence.TransactionRequiredException;
import org.apache.log4j.Logger;
import za.co.pas.business.data.*;
import za.co.pas.business.data.audit.GenericAudit;

/**
 *
 * @author alabuschagne
 */
public class AuditDataModel extends AbstractDataModel
{

    private static Logger logger = Logger.getLogger(AuditDataModel.class);

    public boolean audit(GenericAudit genericAudit)
    {
        try
        {
            em.getTransaction().begin();
            em.persist(genericAudit);
            em.flush();
            em.getTransaction().commit();
            return true;
        }
        catch (IllegalStateException ex)
        {
            logger.error(ex.getMessage(), ex);
        }
        catch (IllegalArgumentException ex)
        {
            logger.error(ex.getMessage(), ex);
        }
        catch (TransactionRequiredException ex)
        {
            logger.error(ex.getMessage(), ex);
        }
        return false;
    }

    public List<GenericAudit> getAuditTrail(Company company, 
                                            UserLoginDetails userLoginDetails,
                                            Calendar start, Calendar end)
    {
        try
        {
            StringBuilder query = new StringBuilder("SELECT c FROM GenericAudit c");
            boolean useWhere = (company != null) || (userLoginDetails != null) || (start != null) || (end != null);
            if (useWhere)
            {
                query.append(" WHERE ");
            }
            if (company != null)
            {
                query.append(" c.company = :company");
            }
            if (userLoginDetails != null)
            {
                query.append(" c.changedBy = :changedBy");
            }
            if (start != null)
            {
                query.append(" c.changedAt >= :changedAtStart");
            }
            if (end != null)
            {
                query.append(" c.changedAt <= :changedAtEnd");
            }

            query.append(" ORDER BY c.changedAt");
            Query q = em.createQuery(query.toString());

            if (company != null)
            {
                q.setParameter("company", company);
            }
            if (userLoginDetails != null)
            {
                q.setParameter("changedBy", userLoginDetails);
            }
            if (start != null)
            {
                q.setParameter("changedAtStart", start);
            }
            if (end != null)
            {
                q.setParameter("changedAtEnd", end);
            }
            
            return q.getResultList();
        }
        catch (NoResultException ex)
        {
            //This is not a error
            logger.info(ex.getMessage());
        }
        catch (NonUniqueResultException ex)
        {
            logger.error(ex.getMessage(), ex);
        }
        catch (IllegalStateException ex)
        {
            logger.error(ex.getMessage(), ex);
        }
        catch (IllegalArgumentException ex)
        {
            logger.error(ex.getMessage(), ex);
        }
        return null;
    }
}
